package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PubAlbumAdapter;
import flc.ast.bean.AlbumBean;
import flc.ast.databinding.ActivityPriAlbumBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class PriAlbumActivity extends BaseAc<ActivityPriAlbumBinding> {

    private PubAlbumAdapter priAlbumAdapter;
    private List<String> listPath = new ArrayList<>();
    private boolean isMultiple = false;
    private Dialog myMoreDialog, myDeleteDialog, myClearDialog;
    private List<AlbumBean> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_pri_album;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPriAlbumBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        priAlbumAdapter = new PubAlbumAdapter();
        priAlbumAdapter.setMultiple(isMultiple);
        mDataBinding.rvPriAlbumList.setLayoutManager(new GridLayoutManager(mContext, 3));
        mDataBinding.rvPriAlbumList.setAdapter(priAlbumAdapter);
        mDataBinding.ivPriAlbumMore.setOnClickListener(this);
        priAlbumAdapter.setOnItemClickListener(this);
        mDataBinding.rlPriAlbumDelete.setOnClickListener(null);
        mDataBinding.ivPriAlbumDelete.setOnClickListener(this);
        mDataBinding.ivPriAlbumAdd.setOnClickListener(this);
        moreDialog();
        deleteDialog();
        clearDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPriAlbumMore:
                myMoreDialog.show();
                break;
            case R.id.tvDialogMoreCancel:
                myMoreDialog.dismiss();
                break;
            case R.id.tvDialogMoreMultiple:
                isMultiple = true;
                priAlbumAdapter.setMultiple(isMultiple);
                priAlbumAdapter.notifyDataSetChanged();
                myMoreDialog.dismiss();
                mDataBinding.rlSelVideoBottom.setVisibility(View.GONE);
                mDataBinding.rlPriAlbumDelete.setVisibility(View.VISIBLE);
                break;
            case R.id.tvDialogMoreClear:
                myMoreDialog.dismiss();
                myClearDialog.show();
                break;
            case R.id.ivPriAlbumDelete:
                if (listPath.size() == 0) {
                    ToastUtils.showShort(R.string.please_sel_delete_video);
                    return;
                }
                myDeleteDialog.show();
                break;
            case R.id.ivDialogDeleteCancel:
                myDeleteDialog.dismiss();
                listPath.clear();
                getData();
                isMultiple = false;
                priAlbumAdapter.setMultiple(isMultiple);
                priAlbumAdapter.notifyDataSetChanged();
                mDataBinding.rlSelVideoBottom.setVisibility(View.VISIBLE);
                mDataBinding.rlPriAlbumDelete.setVisibility(View.GONE);
                break;
            case R.id.ivDialogDeleteConfirm:
                deleteVideo();
                break;
            case R.id.ivDialogClearCancel:
                myClearDialog.dismiss();
                break;
            case R.id.ivDialogClearConfirm:
                for (AlbumBean bean : priAlbumAdapter.getValidData()) {
                    listPath.add(bean.getPath());
                }
                deleteVideo();
                myClearDialog.dismiss();
                break;
            case R.id.ivPriAlbumAdd:
                gotoAlbum();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isMultiple) {
            if (priAlbumAdapter.getItem(position).isChecked()) {
                priAlbumAdapter.getItem(position).setChecked(false);
                listPath.remove(priAlbumAdapter.getItem(position).getPath());
            } else {
                priAlbumAdapter.getItem(position).setChecked(true);
                listPath.add(priAlbumAdapter.getItem(position).getPath());
            }
            priAlbumAdapter.notifyItemChanged(position);
        }else {
            PlayActivity.videoUri=priAlbumAdapter.getItem(position).getPath();
            startActivity(PlayActivity.class);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    //去到系统选择导入视频界面
    private void gotoAlbum() {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT < 19) {
            intent.setAction(Intent.ACTION_GET_CONTENT);
        } else {
            intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
        }
        intent.setType("video/*");
        startActivityForResult(intent, Content.REQUEST_VIDEO_CODE);
    }

    //获取相册数据
    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.Album_Privacy;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new AlbumBean(file.getPath(), "", false));
            }
        }
        if (listShow.size() > 0) {
            priAlbumAdapter.setList(listShow);
            mDataBinding.rvPriAlbumList.setVisibility(View.VISIBLE);
            mDataBinding.llPriAlbumNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPriAlbumList.setVisibility(View.GONE);
            mDataBinding.llPriAlbumNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除相册视频
    private void deleteVideo() {
        showDialog(getString(R.string.delete_video_ing));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                for (int i = 0; i < listPath.size(); i++) {
                    FileUtils.delete(listPath.get(i));
                }
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                myDeleteDialog.dismiss();
                ToastUtils.showShort(R.string.delete_suc);
                getData();
                listPath.clear();
                isMultiple = false;
                priAlbumAdapter.setMultiple(isMultiple);
                priAlbumAdapter.notifyDataSetChanged();
                mDataBinding.rlSelVideoBottom.setVisibility(View.VISIBLE);
                mDataBinding.rlPriAlbumDelete.setVisibility(View.GONE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == Content.REQUEST_VIDEO_CODE) {
            //获取视频路径
            Uri uri = data.getData();
            String resultFile = FileUtil.generateFilePath(Content.Album_Privacy, ".mp4");
            FileP2pUtil.copyToPrivate(uri, resultFile);
            getData();
            ToastUtils.showShort(R.string.import_suc);
        }
    }

    private void moreDialog() {
        myMoreDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_more, null);
        myMoreDialog.setContentView(inflate);
        myMoreDialog.setCancelable(true);
        Window dialogWindow = myMoreDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView cancel = inflate.findViewById(R.id.tvDialogMoreCancel);
        TextView clear = inflate.findViewById(R.id.tvDialogMoreClear);
        TextView multiple = inflate.findViewById(R.id.tvDialogMoreMultiple);
        cancel.setOnClickListener(this);
        clear.setOnClickListener(this);
        multiple.setOnClickListener(this);
    }

    //删除弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(true);
        //获取当前Activity所在的窗体
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogConfirm = inflate.findViewById(R.id.ivDialogDeleteConfirm);
        ImageView dialogCancel = inflate.findViewById(R.id.ivDialogDeleteCancel);
        dialogConfirm.setOnClickListener(this);
        dialogCancel.setOnClickListener(this);
    }

    //删除弹框
    private void clearDialog() {
        myClearDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_clear, null);
        myClearDialog.setContentView(inflate);
        myClearDialog.setCancelable(true);
        //获取当前Activity所在的窗体
        Window dialogWindow = myClearDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogConfirm = inflate.findViewById(R.id.ivDialogClearConfirm);
        ImageView dialogCancel = inflate.findViewById(R.id.ivDialogClearCancel);
        dialogConfirm.setOnClickListener(this);
        dialogCancel.setOnClickListener(this);
    }

}